package org.mwielocha.jxcube.core;

import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import swing2swt.layout.BorderLayout;

public class TrackInfoDialog extends Dialog {

  protected Object result;
  protected Shell shell;
  
  protected ResourceBundle res;
  
  /**
   * Create the dialog.
   * @param parent
   * @param style
   */
  public TrackInfoDialog(Shell parent, int style) {
    super(parent, style);
    setText("SWT Dialog");
    res = ResourceBundle.getBundle("properties/msg");
  }

  /**
   * Open the dialog.
   * @return the result
   */
  public Object open(TableItem item) {
    createContents();
    fillData(item);
    shell.open();
    shell.layout();
    Display display = getParent().getDisplay();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    return result;
  }

  /**
   * Create contents of the dialog.
   */
  private void createContents() {
    shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.RESIZE);
    shell.setSize(486, 344);
    shell.setText(getText());
    shell.setLayout(new BorderLayout(0, 0));
    {
      Composite composite = new Composite(shell, SWT.NONE);
      composite.setLayoutData(BorderLayout.SOUTH);
      RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
      rowLayout.justify = true;
      rowLayout.fill = true;
      rowLayout.center = true;
      composite.setLayout(rowLayout);
      {
        Button okButton = new Button(composite, SWT.NONE);
        okButton.setText(res.getString("general.close"));
        {
          {
            mainTable = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
            mainTable.setHeaderVisible(false);
            mainTable.setLinesVisible(true);
            {
              TableColumn tableColumn = new TableColumn(mainTable, SWT.NONE);
              tableColumn.setWidth(120);
              tableColumn.setText("New Column");
            }
            {
              TableColumn tableColumn = new TableColumn(mainTable, SWT.NONE);
              tableColumn.setWidth(500);
              tableColumn.setText("New Column");
            }
          }
        }
        okButton.addSelectionListener(new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            shell.dispose();
          }
        });
      }
    }

  }
  
  private static final String[] NAMES = {
    "mainWindow.mainTable.trackColumn",
    "mainWindow.mainTable.titleColumn",
    "mainWindow.mainTable.artistColumn",
    "mainWindow.mainTable.albumColumn",
    "mainWindow.mainTable.genreColumn",
    "mainWindow.mainTable.timeColumn",
    "general.path" };
  private Table mainTable;
    
  
  protected void fillData(TableItem item) {
    shell.setText(item.getText(1));
    int i = 0;
    for(String name : NAMES) {
      new TableItem(mainTable, SWT.NONE).setText(new String[] { 
          res.getString(name), item.getText(i) });
      i++;
    }
  }
}
